package mws;

import java.util.*;

public class Main {

    public static void main(String[] args) {

  new Main().myAtoi("-91283472332");
        System.out.println("\n重试流程结束");


//        Scanner cin = new Scanner(System.in);
//
//        System.out.println();
//        int n ;
//        n = Integer.parseInt(cin.nextLine());
//        int[] length;
//        String s = cin.nextLine();
//        String[] s1 = s.split(" ");
//        length = new int[s1.length];
//        for (int i = 0; i < s1.length; i++) {
//            length[i] = Integer.parseInt(s1[i]);
//        }
//
//        System.out.println(n);
//        for (int i = 0; i < length.length; i++) {
//            System.out.print(length[i]);
//        }

    }

    public int myAtoi(String s) {

        int length = s.length();
        int flag = 0;
        int res = 0;
        int pow = 0;
        int count = 0;
        int start = 0;
        int flagOver = 1;
        for (int i = 0; i < length; i++) {

            char c = s.charAt(i);
            if(c == ' '){
                continue;
            }else if(c <= '9' && c >= '0'){
                start = 1;
                int resIn = c - '0';
                long  temp= 0;
                temp =  res * 10l+ resIn;
                if(temp > (long) Integer.MAX_VALUE){
                    flagOver = 0;
                break;
                }else if(temp < Integer.MIN_VALUE){
                    flagOver = 2;
                break;
                }
                res = (int) temp;
                pow ++ ;
            }else {
                if(c == '+'){
                    flag = 0;
                    if(start == 1){
                        break;
                    }
                }else if (c == '-'){
                    flag = 1;
                    if(start == 1){
                        break;
                    }
                }else {
                    break;
                }
            }

        }
if(flagOver == 1){
    if (flag == 1)
        res = -res;
}else if(flagOver == 2){
    return Integer.MIN_VALUE;
}else if (flagOver == 0){
    return Integer.MAX_VALUE;
}
        return res;


    }
    public String Solution(String enter) {

        byte[] bytes = enter.getBytes();

        StringBuilder result = new StringBuilder();
        HashMap<Integer, String> hashMap = new HashMap<>();
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < bytes.length; i++) {

            if (bytes[i] == '[') {
                linkedList.addLast(i);
            } else if (bytes[i] == ']') {
                Integer integer = linkedList.pollLast();
                StringBuilder stringBuilder = new StringBuilder();
                int j = integer + 1;
                for (; ; j++) {
                    if (bytes[j] != '|') {
                        stringBuilder.append((char) bytes[j]);
                    } else break;
                }

                int num = Integer.parseInt(stringBuilder.toString());
                j++;
                StringBuilder stringBuilder1 = new StringBuilder();
                while (j < i) {

                    if(bytes[j] != '['){
                        stringBuilder1.append((char) bytes[j]);
                    }else {
                        String s = hashMap.get(j);
                        stringBuilder1.append(s);
                        int count = 1;
                        while (count != 0){
                            j++;
                            if(bytes[j] == '['){
                                count++;
                            }else if(bytes[j] == ']'){
                                count--;
                            }

                        }
                    }
                    j++;
                }
                StringBuilder stringBuilder2 = new StringBuilder();
                for (int k = 0; k < num; k++) {
                    stringBuilder2.append(stringBuilder1.toString());
                }
                hashMap.put(integer,stringBuilder2.toString());
                if(linkedList.size() == 0)
                {

                    result.append(stringBuilder2.toString());
                }
            }else if( linkedList.size() == 0){
                result.append((char) bytes[i]);
            }
        }

        return result.toString();


    }

}


//leetcode submit region begin(Prohibit modification and deletion)
class Solution1 {

    public static void main(String[] args) {

        String s = new Solution1().longestPalindrome("aaaa");
    }
    public String longestPalindrome(String s) {

        int count = -1;
        String result = "";
        byte[] bytes = s.getBytes();

        int mid = 0;
        int record = 1;
        for (int i = 1; i < bytes.length-1; i++) {
            if(i - record < 0 || i + record > bytes.length - 1)
            {
                continue;
            } else if(bytes[i-record] != bytes[i+record]){
                continue;
            }

            if(bytes[i-1] == bytes[i+1]){
                int equal = 1;
                int step = 2;
                while ( (i - step) >= 0 &&  (i+step) <= bytes.length - 1){

                    if(bytes[i - step] == bytes [i + step]){
                        equal++;
                    }else {
                        break;
                    }
                    step++;
                }
                int sum = equal * 2 + 1;
                if(sum > count) {
                        step--;
                    count = sum;
                    StringBuilder stringBuilder = new StringBuilder();

                    for (int j = i - step; j <= i+step ; j++) {
                        stringBuilder.append((char) bytes[j]);
                    }

                    result = stringBuilder.toString();
                }
                record = Math.max(record,equal);
            }
        }
        record =   (count + 1) / 2  - 1;

        for (int i = 0; i < bytes.length-1; i++) {
            if (i - record < 0 || i + record + 1 > bytes.length-1 ) {

                continue;

            }else  if (bytes[i - record] != bytes[i + record + 1]) {

                continue;
            }

            if (bytes[i] == bytes[i+1]) {
                int equal = 1;
                int step = 1;
                while ((i - step) >= 0 && (i + step + 1) <= bytes.length -1) {

                    if (bytes[i - step] == bytes[i + step + 1]) {
                        equal++;
                    } else {
                        break;
                    }
                    step++;
                }
                int sum = equal * 2 ;
                if (sum > count) {
                        step--;
                    count = sum;
                    StringBuilder stringBuilder = new StringBuilder();

                    for (int j = i - step  ; j <= i + step + 1 ; j++) {
                        stringBuilder.append((char) bytes[j]);
                    }

                    result = stringBuilder.toString();
                    record = Math.max(record, equal);
                }

            }
        }
        if(count <= 1){
            result = String.valueOf((char) bytes[0]);
        }
        return result;
    }


}


